package org.agilelang.generator.webservices.php;

/*
 AgileLang - A fourth generation language(http://www.agilelang.org).

 Copyright (C) 2013 Roderick Cusirramos (roderick20@hotmail.com)

 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 2 of the License, or
 (at your option) any later version.

 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.

 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import org.agilelang.mvc.Generator;
import org.agilelang.mvc.IFiles;
import org.agilelang.object.Application;

public class GFile extends Generator implements IFiles {

    public GFile(String path, Application app) {
        this.path = path;
        this.app = app;
        this.template = "bootstrap";
    }

    @Override
    public void process() {
        directoriesStatic();
        filesStatic();
        directoriesStatic_slim();
        filesStatic_slim();
    }

    public void directoriesStatic() {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/org/agilelang/generator/webservices/php/bootstrap/files/_directories.xml");
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                }
            }
        } catch (Exception e) {
        }
        JAXBContext jc;
        StaticDirectories staticDirectories = null;
        try {
            jc = JAXBContext.newInstance(StaticDirectories.class);
            Unmarshaller u = jc.createUnmarshaller();
            staticDirectories = (StaticDirectories) u.unmarshal(reader);
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        List<StaticDirectory> directories = staticDirectories.directories;
        for (StaticDirectory directory : directories) {
            this.createDirectory(path + directory.path);
            System.out.println(directory.path);
        }
    }

    public void filesStatic() {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/org/agilelang/generator/webservices/php/bootstrap/files/_files.xml");
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JAXBContext jc;
        StaticFiles staticFiles = null;
        try {
            jc = JAXBContext.newInstance(StaticFiles.class);
            Unmarshaller u = jc.createUnmarshaller();
            staticFiles = (StaticFiles) u.unmarshal(reader);
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        List<StaticFile> files = staticFiles.files;
        for (StaticFile file : files) {
            System.out.println(file.origin);
            if (file.type.equals("file")) {
                fileRelocateFile("webservices/php", file.origin, path + file.destination);
            }
            if (file.type.equals("imagen")) {
                createImageIcon("webservices/php", file.origin, path + file.destination);
            }
            //fileRelocateFile("index_root.jsp", path + "/web/index.jsp");
        }
    }

    public void directoriesStatic_slim() {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/org/agilelang/generator/webservices/php/bootstrap/files/slim/_directories.xml");
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                }
            }
        } catch (Exception e) {
        }
        JAXBContext jc;
        StaticDirectories staticDirectories = null;
        try {
            jc = JAXBContext.newInstance(StaticDirectories.class);
            Unmarshaller u = jc.createUnmarshaller();
            staticDirectories = (StaticDirectories) u.unmarshal(reader);
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        List<StaticDirectory> directories = staticDirectories.directories;
        for (StaticDirectory directory : directories) {
            this.createDirectory(path + directory.path);
            System.out.println(directory.path);
        }
    }

    public void filesStatic_slim() {
        Reader reader = null;
        try {
            InputStream is = this.getClass().getResourceAsStream("/org/agilelang/generator/webservices/php/bootstrap/files/slim/_files.xml");
            if (is != null) {
                try {
                    reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        JAXBContext jc;
        StaticFiles staticFiles = null;
        try {
            jc = JAXBContext.newInstance(StaticFiles.class);
            Unmarshaller u = jc.createUnmarshaller();
            staticFiles = (StaticFiles) u.unmarshal(reader);
        } catch (JAXBException ex) {
            System.err.println("Error al poblar objeto Project : " + ex.toString());
        }

        List<StaticFile> files = staticFiles.files;
        for (StaticFile file : files) {
            System.out.println(file.origin);
            if (file.type.equals("file")) {
                fileRelocateFile("webservices/php", "slim/" + file.origin, path + file.destination);
            }
            if (file.type.equals("imagen")) {
                createImageIcon("webservices/php", "slim/" + file.origin, path + file.destination);
            }
            //fileRelocateFile("index_root.jsp", path + "/web/index.jsp");
        }
    }
}
